####################################
# global libraries used everywhere #
####################################

# Note: Using this with a specific version number may fail, since not all dependencies might be met.
# Debug interactively, then identify all installed packages that needed to be pinned.

pkgTest <- function(x,y="")
{
  if (!require(x,character.only = TRUE))
  {
    if ( y == "" ) 
    {
      install.packages(x,dep=TRUE)
    } else {
      remotes::install_version(x, y)
    }
    if(!require(x,character.only = TRUE)) stop("Package not found")
  }
  return("OK")
}


global.libraries <- c('sf','httr','utils','tools','stringr','Hmisc',
                      'readr','data.table','parallel','foreach',
					  'doParallel','sf','foreign','nngeo',
                      'tidyr','renv','ggplot2','R.utils',
                      'RColorBrewer','archive','terra','ordinal',
                      'modelsummary','xfun','gt','stars','dplyr',
                      'XML','colourvalues','ggpubr','R.utils','raster',
                      'patchwork','rgdal','readstata13','rgeos',
					  'lwgeom','fasterize','gdistance','doSNOW',
					  'exactextractr','ggplot2','ggpattern','readxl','rSDM',
					  'purrr','openxlsx','spatialEco')

print("caret and plyr cause incorrect dplyr: group by")
print("when loaded before dplyr")

results <- sapply(as.list(global.libraries), pkgTest)

#remotes::install_github("Pakillo/rSDM")

print(.libPaths())

print(sessionInfo())

print(version)